/*
 * MainWindowMirror2.java
 *
 * Created on 30 April 2007, 15:30
 *
 *
 */

package krakow.ae.knp.wikiboard.gui;

import krakow.ae.knp.wikiboard.gui.Board;

import krakow.ae.knp.wikiboard.gui.tools.markers.FigureMarkerFactory;

import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class MainWindowMirror2 extends JFrame {
    
    Map component_map;
    public static Board board = null;
    JTextField tf;
    
    public MainWindowMirror2() {
        
        component_map = new ComponentMap();
        GUI gui = new GUI();
        
        setLayout(new FlowLayout());
//        gui.btDrawElipse.setName("btDrawElipse");
//        gui.btDrawRectangle.setName("btDrawRectangle");
//        gui.btDrawStraightLine.setName("btStraightLine");
//        gui.btDrawLine.setName("btDrawLine");
//        gui.btSelect.setName("btSelect");
//        gui.btClearLast.setName("btClearLast");
//        gui.btRedoLast.setName("btRedoLast");
//        gui.btClearAll.setName("btClearAll");
//        add(gui.board);
//        add(gui.btDrawElipse);
//        add(gui.btDrawRectangle);
//        add(gui.btDrawStraightLine);
//        add(gui.btDrawLine);
//        add(gui.btSelect);
//        add(gui.btClearLast);
//        add(gui.btRedoLast);
//        add(gui.btClearAll);
    }
    
    public void openSender(Socket sock) throws Exception {
        System.out.println("client");
        final ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());
        
        Toolkit.getDefaultToolkit().addAWTEventListener(
                new AWTEventListener() {
            public void eventDispatched(AWTEvent evt) {
                try {
                    if(evt instanceof MouseEvent) {
                        MouseEvent me = (MouseEvent)evt;
                        System.out.println("Wysylam:" + me.getComponent().getName());
                        out.writeObject(me.getComponent().getName());
                        out.writeObject(evt);
                    }
                } catch (Exception ex) { }
            }
        },
                AWTEvent.ACTION_EVENT_MASK |
                AWTEvent.MOUSE_EVENT_MASK
                );
    }
    
    public void openReceiver() throws Exception {
        // receive events
        System.out.println("couldn't open socket. must be the server");
        ServerSocket server = new ServerSocket(6754);
        Socket sock = server.accept();
        
        EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
        
        ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
        while(true) {
            String id = (String) in.readObject();
            AWTEvent evt = (AWTEvent) in.readObject();
            System.out.println("Odebralem:" + evt);
            if(evt instanceof MouseEvent) {
                MouseEvent me = (MouseEvent)evt;
                //System.out.println("Odebralem:" + me.getComponent().getName()); WTF!!?!??!?
                MouseEvent me2 = new MouseEvent(
                        (Component)component_map.get(id),
                        me.getID(),
                        me.getWhen(),
                        me.getModifiers(),
                        me.getX(),
                        me.getY(),
                        me.getClickCount(),
                        me.isPopupTrigger(),
                        me.getButton()
                        );
                eq.postEvent(me2);
            }
        }
    }
    
    public void start() {
        try {
            // send events
            final Socket sock = new Socket("localhost",6754);
            openSender(sock);
        } catch (Exception ex) {
            try {
                openReceiver();
            } catch (Exception ex2) {
                System.out.println("exception: " + ex);
            }
        }
    }
    
    public void start2() {
        try {
            openReceiver();
        } catch (Exception ex) {
            
            System.out.println("exception: " + ex);
        }
    }
    
    public void start3() {
        try {
            // send events
            final Socket sock = new Socket("localhost",6754);
            openSender(sock);
        } catch (Exception ex) {
            
            System.out.println("exception: " + ex);
        }
    }
    
    public static void main(String[] args) {
        //MainWindowMirror2 m = new MainWindowMirror2();
        //SwingConsole.run(new MainWindow2(), 700, 700,"");
        //SwingConsole.run(m, 500, 500,"");
        // m.start();
        System.out.println("AAA");
    }
}

